package com.cool.reggie.modules.category.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cool.reggie.modules.category.entity.CategoryEntity;
import com.cool.reggie.modules.category.service.CategoryService;
import com.cool.reggie.utils.PageUtils;
import com.cool.reggie.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-06
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
        private CacheManager cacheManager;

    @Autowired
    private CategoryService categoryService;

    @CachePut(value="categoryCache",key="#category.id")
    @PostMapping
    public ResultData addCategoryType(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return ResultData.ok();
    }
    @CacheEvict(value="categoryCache",key="#category.id")
    @PutMapping
    public ResultData updateCategory(@RequestBody CategoryEntity category) {
        boolean result = categoryService.updateById(category);
        return ResultData.ok();
    }
    @CacheEvict(value="categoryCache",key="#id")
    @DeleteMapping
    public ResultData deleteCategory(Long id) {
        boolean result = categoryService.removeCategory(id);
        return ResultData.ok();
    }

    @GetMapping(value = {"/page", "/page1"})
    public ResultData getCategoryList(int page, int pageSize) {
        PageUtils pageUtils = categoryService.queryCategoryPage(page, pageSize);
        return ResultData.ok(pageUtils);
    }

    @GetMapping("/list")
    public ResultData selectList(CategoryEntity category) {
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null, CategoryEntity::getType, category.getType());
        List<CategoryEntity> list = categoryService.list(queryWrapper);
        return ResultData.ok(list);
    }

}
